#coding:utf8
import os
import csv
import random
import string
import networkx as nx
import matplotlib
from matplotlib import pyplot
#from __future__ import division
tseries=[]

# currentdirpath =os.getcwd()
# filename =' graphdata.csv'
# file_path=os.path.join(currentdirpath, filename) #file_path to open
def get_file_path(filename):
    currentdirpath =os.getcwd()
    file_path=os.path.join(currentdirpath, filename)
    #print file_path
    return file_path

# path= get_file_path('timeseries.csv')
# print path

natlist=[]
result=[]
def read_csv(filepath):
    with open(filepath, 'rU') as  csvfile:
        reader =csv.reader(csvfile)
        for row in reader:
#             print row[1]
            natlist.append(row[1])
#             print "nat=",natlist
        return natlist
lit=[]

def horizon_visibility(tseries):
    for i in range(0,len(tseries)-1):
        (ta,ya)=tseries[i]
        for n in range(i+1,len(tseries)):
            (tb,yb)=tseries[n]
            if(yb==ya):
                lit.append([ta,tb])
    print "lit=",lit

    return lit

def node_graph(tup):
    h=nx.Graph()
    h.add_edges_from(tup)
    print "hi"
    print "edges:" ,h.edges()
    #%matplotlib inline
    BLUE="#99CCFF"
    nx.draw(h, node_color=BLUE,with_labels=True)
    pyplot.show()

def tup_series(tup):
    print "Tup is here", tup[1],tup[2]
    n=1
    for magnitude in tup:
        tseries.append((n,magnitude))
        n=n+1
    print "Tseries = ", tseries
    return tseries


def create(ts):
    edges = []
    i, j, second = 0, 1, None
    while i < (len(ts) - 1):
        if j==1: second = ts[i + j]
        
        '''遇到更高点， i前进'''
        if ts[i + j] >= ts[i]:
            edges.append((i, i + j))
            i, j = i + 1, 1
        else:
            '''可视检测'''
            if ts[i + j] >= second: 
                second = ts[i + j]
                edges.append((i, i + j))
            j += 1
            '''虽未遇到高点，但已末尾'''
            if i + j == (len(ts)): i, j = i + 1, 1
        '''End else'''
    '''End while'''
        
    for e in edges:
        print e[0] + 1, e[1] + 1     
        
    return [[e[0], e[1]] for e in edges]
'''End def create'''   
        
# tup = read_csv('timeseries.csv')
# print tup
tup = [0.9, 0.5, 0.4, 0.8, 0.9, 0.5, 0.4, 0.8, 0.9, 0.5, 0.4, 0.8, 0.9, 0.5, 0.4, 0.8, 0.9, 0.5, 0.4, 0.8]
# tup = [(i, d[i]) for i in range(len(d))]
print tup

# tseries = tup_series(tup)

# li = horizon_visibility(tseries)
li = create(tup)

node_graph(li);
#print tup[0],tup[1]


